#include "inputdlg.h"

InputDlg::InputDlg(QDialog *parent) : QDialog(parent)
{
    nameLabel1 = new QLabel(tr("name :"));
    nameLabel2 = new QLabel(tr("twisted"));
    nameLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    nameButton = new QPushButton(tr("change name"));
    connect(nameButton, &QPushButton::clicked, this, &InputDlg::changeName);

    sexLabel1 = new QLabel(tr("sex :"));
    sexLabel2 = new QLabel(tr("man"));
    sexLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    sexButton = new QPushButton(tr("change sex"));
    connect(sexButton, &QPushButton::clicked, this, &InputDlg::changeSex);

    ageLabel1 = new QLabel(tr("age :"));
    ageLabel2 = new QLabel(tr("25"));
    ageLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    ageButton = new QPushButton(tr("change age"));
    connect(ageButton, &QPushButton::clicked, this, &InputDlg::changeAge);

    scoreLabel1 = new QLabel(tr("score :"));
    scoreLabel2 = new QLabel(tr("100"));
    scoreLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    scoreButton = new QPushButton(tr("change score"));
    connect(scoreButton, &QPushButton::clicked, this, &InputDlg::changeScore);

    mainLayout = new QGridLayout(this);

    mainLayout->addWidget(nameLabel1, 0, 0);
    mainLayout->addWidget(nameLabel2, 0, 1);
    mainLayout->addWidget(nameButton, 0, 2);
    mainLayout->addWidget(sexLabel1, 1, 0);
    mainLayout->addWidget(sexLabel2, 1, 1);
    mainLayout->addWidget(sexButton, 1, 2);
    mainLayout->addWidget(ageLabel1, 2, 0);
    mainLayout->addWidget(ageLabel2, 2, 1);
    mainLayout->addWidget(ageButton, 2, 2);
    mainLayout->addWidget(scoreLabel1, 3, 0);
    mainLayout->addWidget(scoreLabel2, 3, 1);
    mainLayout->addWidget(scoreButton, 3, 2);
}

// 标准字符串输入框
void InputDlg::changeName()
{
    // 字符串输入框由getText()函数来完成
    bool ok;
    QString name = QInputDialog::getText(this, tr("字符串输入框"), tr("plz input the name"), QLineEdit::Normal, nameLabel2->text(), &ok);

    if (ok && !name.isEmpty())
    {
        nameLabel2->setText(name);
    }
}

// 标准条目选择对话框
void InputDlg::changeSex()
{
    // 条目选择对话框由getItem()函数来完成
    // 下拉列表
    QStringList sexItems;
    sexItems << tr("man") << tr("women");

    bool ok;
    QString sex = QInputDialog::getItem(this, tr("条目选择框"), tr("plz select the sex"), sexItems, 0, false, &ok);
    if (ok && !sex.isEmpty())
    {
        sexLabel2->setText(sex);
    }
}

// 标准int类型输入框
void InputDlg::changeAge()
{
    bool ok;
    int age = QInputDialog::getInt(this, tr("整形输入框"), tr("plz intput the age"), ageLabel2->text().toInt(&ok), 0, 100, 1, &ok);

    if (ok)
    {
        ageLabel2->setText(tr("%1").arg(age));
    }
}

void InputDlg::changeScore()
{
    bool ok;
    double score = QInputDialog::getDouble(this, tr("浮点型输入框"), tr("plz intput the score"), scoreLabel2->text().toDouble(&ok), 0, 100, 1, &ok);

    if (ok)
    {
        scoreLabel2->setText(tr("%1").arg(score));
    }
}
